﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace HuluFeedFinder
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient myClient = new WebClient();
            byte[] abStreamData = new byte[100000];
            bool bFoundIt = false;
            int iNumToCheck;

            Console.Write("Name of the show to look for: ");
            string sShowName = Console.ReadLine();

            Console.Write("Number of records to check (not sure? try 20000) : ");
            while (!Int32.TryParse(Console.ReadLine(), out iNumToCheck))
            {
                Console.Write("Invalid input.");
                Console.Write("Number of records to check (not sure? try 20000) : ");
            }

            for (int i = 0; i < iNumToCheck && !bFoundIt; i++)
            {
                string sURL = @"http://www.hulu.com/feed/show/" + i.ToString() + "/episodes";

                try
                {
                    using (Stream strFeed = myClient.OpenRead(sURL))
                    {
                        Console.WriteLine("Checking {0}", sURL);

                        strFeed.Read(abStreamData, 0, 100000);
                        string sTheFeed = Encoding.ASCII.GetString(abStreamData);

                        int iStartTitle = sTheFeed.IndexOf("<title>");
                        int iEndTitle = sTheFeed.IndexOf("</title>");
                        string sFeedShowTitle = sTheFeed.Substring(iStartTitle + 7, iEndTitle - iStartTitle - 7);

                        int iIndex = sFeedShowTitle.IndexOf(sShowName);
                        if (iIndex > 0)
                        {
                            Console.WriteLine("    Found " + sShowName + " in this feed: " + sFeedShowTitle);
                            Console.WriteLine("    " + sURL);
                            Console.Write("--- Do you want to continue searching? (Y/N) ");
                            char sOpt = Console.ReadKey().KeyChar;
                            if (sOpt.ToString().ToUpper() == "N")
                            {
                                bFoundIt = (sOpt == 'N');
                                Console.Write("\n");        // Insert newline
                            }
                        }
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError &&
                        ex.Response != null)
                    {
                        var resp = (HttpWebResponse)ex.Response;
                        if (resp.StatusCode == HttpStatusCode.NotFound)
                            Console.WriteLine("  Feed {0} not found - skipping.", i.ToString());
                        else
                            Console.WriteLine("  Unknown error retrieving the feed: " + ex.InnerException);
                    }
                    else
                        Console.WriteLine("  Unknown error retrieving the feed: " + ex.InnerException);
                }
            }

            Console.WriteLine("\nThe program has ended.  Press any key to close");
            Console.ReadKey();
        }
    }
}
